import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;

/**
 * Created by 罗天瑞 on 2016/10/29.
 */
public class EnlargeImage {
    public void EnlargeImage()
    {
        try
        {
            BufferedImage image = ImageIO.read(new File("data/wallpaper.jpg"));
            int Width = image.getWidth();
            int Height = image.getHeight();
            int[] ImageArray =  new int[Width*Height];
            int NewWidth = Width*2;
            int NewHeight = Height*2;
            int[] NewImageArray = new int[NewWidth*NewWidth];
            image.getRGB(0,0,Width,Height,ImageArray,0,Width);

            for(int j=0;j<Height-1;j++)
            {
                for(int i=0;i<Width-1;i++)
                {
                    int x0 = 2*i;
                    int y0 = 2*j;
                    int x1,y1;
                    if( i==Width-2)
                    {
                        x1=NewWidth-1;
                    }
                    else
                    {
                        x1=(i+1)*2;
                    }
                    if(j==Height-2)
                    {
                        y1=NewHeight-1;
                    }
                    else
                    {
                        y1=(j+1)*2;
                    }
                    int d1= x1-x0;
                    int d2 = y1 -y0;
                    if(0 == NewImageArray[y0*NewWidth + x0])
                    {
                        NewImageArray[y0*NewWidth + x0] =  ImageArray[j*Width+i];
                    }
                    if(0 == NewImageArray[y0*NewWidth + x1])
                    {
                        if(i == Width-2)
                        {
                            NewImageArray[y0*NewWidth + x1] = ImageArray[j*Width+Width-1];
                        }
                        else
                        {
                            NewImageArray[y0*NewWidth + x1] =  ImageArray[j*Width+i+1];
                        }
                    }
                    if(0 == NewImageArray[y1*NewWidth + x0])
                    {
                        if(j == Height-2)
                        {
                            NewImageArray[y1*NewWidth + x0] = ImageArray[(Height-1)*Width+i];
                        }
                        else
                        {
                            NewImageArray[y1*NewWidth + x0] =  ImageArray[(j+1)*Width+i];
                        }
                    }
                    if(0 == NewImageArray[y1*NewWidth + x1])
                    {
                        if(i==Width-2 && j==Height-2)
                        {
                            NewImageArray[y1*NewWidth + x1] = ImageArray[(Height-1)*Width+Width-1];
                        }
                        else
                        {
                            NewImageArray[y1*NewWidth + x1] =ImageArray[(j+1)*Width+i+1];
                        }
                    }
                    int r, g, b;
                    float c;
                    ColorModel cm = ColorModel.getRGBdefault();
                    for(int l=0; l<d2; l++)
                    {
                        for(int k=0; k<d1; k++)
                        {
                            if(0 == l)
                            {
                                if(j<Height-1 && NewImageArray[y0*NewWidth + x0 + k] == 0)
                                {
                                    c = (float)k/d1;
                                    r = cm.getRed(NewImageArray[y0*NewWidth + x0]) + (int)(c*(cm.getRed(NewImageArray[y0*NewWidth + x1]) - cm.getRed(NewImageArray[y0*NewWidth + x0])));
                                    g = cm.getGreen(NewImageArray[y0*NewWidth + x0]) + (int)(c*(cm.getGreen(NewImageArray[y0*NewWidth + x1]) - cm.getGreen(NewImageArray[y0*NewWidth + x0])));
                                    b = cm.getBlue(NewImageArray[y0*NewWidth + x0]) + (int)(c*(cm.getBlue(NewImageArray[y0*NewWidth + x1]) - cm.getBlue(NewImageArray[y0*NewWidth + x0])));
                                    NewImageArray[y0*NewWidth + x0 + k] = 255<<24 | r<<16 | g<<8 | b;
                                }
                                if(j+1<Height && NewImageArray[y1*NewWidth + x0 + k] == 0)
                                {
                                    c = (float)k/d1;
                                    r = cm.getRed(NewImageArray[y1*NewWidth + x0]) + (int)(c*(cm.getRed(NewImageArray[y1*NewWidth + x1]) - cm.getRed(NewImageArray[y1*NewWidth + x0])));
                                    g = cm.getGreen(NewImageArray[y1*NewWidth + x0]) + (int)(c*(cm.getGreen(NewImageArray[y1*NewWidth + x1]) - cm.getGreen(NewImageArray[y1*NewWidth + x0])));
                                    b = cm.getBlue(NewImageArray[y1*NewWidth + x0]) + (int)(c*(cm.getBlue(NewImageArray[y1*NewWidth + x1]) - cm.getBlue(NewImageArray[y1*NewWidth + x0])));
                                    NewImageArray[y1*NewWidth + x0 + k] = 255<<24 | r<<16 | g<<8 | b;
                                }
                            }
                            else
                            {

                                c = (float)l/d2;
                                r = cm.getRed(NewImageArray[y0*NewWidth + x0+k]) + (int)(c*(cm.getRed(NewImageArray[y1*NewWidth + x0+k]) - cm.getRed(NewImageArray[y0*NewWidth + x0+k])));
                                g = cm.getGreen(NewImageArray[y0*NewWidth + x0+k]) + (int)(c*(cm.getGreen(NewImageArray[y1*NewWidth + x0+k]) - cm.getGreen(NewImageArray[y0*NewWidth + x0+k])));
                                b = cm.getBlue(NewImageArray[y0*NewWidth + x0+k]) + (int)(c*(cm.getBlue(NewImageArray[y1*NewWidth + x0+k]) - cm.getBlue(NewImageArray[y0*NewWidth + x0+k])));
                                NewImageArray[(y0+l)*NewWidth + x0 + k] = 255<<24 | r<<16 | g<<8 | b;
                            }
                        }
                        if(i==Width-2 || l==d2-1)
                        {
                            c = (float)l/d2;
                            r = cm.getRed(NewImageArray[y0*NewWidth + x1]) + (int)(c*(cm.getRed(NewImageArray[y1*NewWidth + x1]) - cm.getRed(NewImageArray[y0*NewWidth + x1])));
                            g = cm.getGreen(NewImageArray[y0*NewWidth + x1]) + (int)(c*(cm.getGreen(NewImageArray[y1*NewWidth + x1]) - cm.getGreen(NewImageArray[y0*NewWidth + x1])));
                            b = cm.getBlue(NewImageArray[y0*NewWidth + x1]) + (int)(c*(cm.getBlue(NewImageArray[y1*NewWidth + x1]) - cm.getBlue(NewImageArray[y0*NewWidth + x1])));
                            NewImageArray[(y0+l)*NewWidth + x1] = 255<<24 | r<<16 | g<<8 | b;
                        }
                    }
                }
            }

            BufferedImage NewImage = new BufferedImage(NewWidth,NewHeight,BufferedImage.TYPE_INT_RGB);
            NewImage.setRGB(0,0,NewWidth,NewHeight,NewImageArray,0,NewWidth);
            File output = new File("data/EnlargeWallPaper.jpg");
            ImageIO.write(NewImage,"jpg",output);

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
